package com.example.k.littletag.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.k.littletag.R;
import com.example.k.littletag.component.RevealBackgroundView;
import com.example.k.littletag.db.NoteDB;
import com.example.k.littletag.utils.Note;

/**
 * Created by Kong on 2015/10/1.
 */
public class SaveNote extends Activity implements View.OnClickListener {
    public static final String ARG_REVEAL_START_LOCATION = "reveal_start_location";
    public static final int TIME_RETURN = 1;
    public static final String REWRITE = "rewrite";
    public static final String CHECK_COUNT = "check_count";
    public static final String CONTENT_SAVE = "content_save";
    public static final String TIME_PICKED = "time_picked";


    private RevealBackgroundView revealView;
    private RelativeLayout rLayoutSaveNote;
    private CardView noteKeepCardView;
    private CardView noteSaveCardView;
    private Button setTimeButton;
    private TextView saveNoteView;
    private EditText editNote;
    private CheckBox[] checkbox = new CheckBox[5];

    private String content;
    private int[] time;
    private int checkCount = 2;
    private NoteDB noteDB;


    public static void start(int[] location, Activity startingActivity) {
        Intent intent = new Intent(startingActivity, SaveNote.class);
        intent.putExtra(ARG_REVEAL_START_LOCATION, location);
        intent.putExtra(REWRITE, 0);
        startingActivity.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keep_note_activity);

        initView();
        initCheckView();
        initAction();
        initRevealView(savedInstanceState);

    }


    private void initAction() {
        setTimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                v.getLocationInWindow(location);
                location[0] += v.getMeasuredWidth() / 2;
                location[1] += v.getMeasuredHeight() / 2;
                Intent intent = new Intent(getApplication(), PickTime.class);
                intent.putExtra(ARG_REVEAL_START_LOCATION, location);
                startActivityForResult(intent, TIME_RETURN);
            }
        });

        saveNoteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (editNote.getText().toString().equals("")) {
                    Toast.makeText(getApplication(), "不能保存一条空备忘录", Toast.LENGTH_SHORT).show();

                } else {
                    /**
                     * 通过content将原数据库的记录删除，再保存新的纪录，很烂的写法
                     */
                    Note note = new Note();
                    note.setContent(content);
                    if (getIntent().getIntExtra(REWRITE,-1)==1) {
                        noteDB.deleteNote(note);
                    }
                    note.setCheckCount(checkCount);
                    note.setTime(time);
                    note.setContent(editNote.getText().toString());
                    noteDB.saveNote(note);

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }

            }
        });

    }

    private void initCheckView() {
        checkbox[0].setOnClickListener(this);
        checkbox[1].setOnClickListener(this);
        checkbox[2].setOnClickListener(this);
        checkbox[3].setOnClickListener(this);
        checkbox[4].setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TIME_RETURN) {
            switch (resultCode) {
                case RESULT_OK:
                    time = data.getIntArrayExtra(TIME_PICKED);
                    setTimeButton.setTextColor(getResources().getColor(R.color.reveal_blue));
                    setTimeButton.setText(time[0] + "-" + time[1] + "-" + time[2] + " " + time[3] + " : " + time[4]);
                    break;
                case RESULT_CANCELED:
                    break;
            }
        }

    }

    private void initView() {
        time = new int[6];

        noteDB = NoteDB.getNoteDB(this);
        setTimeButton = (Button) findViewById(R.id.set_time);
        saveNoteView = (TextView) findViewById(R.id.save_note);
        editNote = (EditText) findViewById(R.id.edit_note_save);
        noteKeepCardView = (CardView) findViewById(R.id.note_keep_cardView);
        noteSaveCardView = (CardView) findViewById(R.id.note_save_cardView);
        rLayoutSaveNote = (RelativeLayout) findViewById(R.id.rLayout_save_note);
        revealView = (RevealBackgroundView) findViewById(R.id.revealViewSaveNote);
        checkbox[0] = (CheckBox) findViewById(R.id.checkbox_1);
        checkbox[1] = (CheckBox) findViewById(R.id.checkbox_2);
        checkbox[2] = (CheckBox) findViewById(R.id.checkbox_3);
        checkbox[3] = (CheckBox) findViewById(R.id.checkbox_4);
        checkbox[4] = (CheckBox) findViewById(R.id.checkbox_5);

        if (getIntent().getIntExtra(REWRITE, -1) == 1) {
            time = getIntent().getIntArrayExtra(TIME_PICKED);
            checkCount = getIntent().getIntExtra(CHECK_COUNT, -1);
            content = getIntent().getStringExtra(CONTENT_SAVE);
            if (time[0] == 0 && time[1] == 0 && time[2] == 0) {
                setTimeButton.setText("设置时间");
            } else {
                setTimeButton.setText(time[0] + "-" + time[1] + "-" + time[2] + " " + time[3] + ":" + time[4]);
            }
            editNote.setText(content);

            checkbox[0].setChecked(false);
            checkbox[1].setChecked(false);
            checkbox[2].setChecked(false);
            checkbox[3].setChecked(false);
            checkbox[4].setChecked(false);
            switch (checkCount) {
                case R.id.checkbox_5:
                    checkbox[4].setChecked(true);
                    checkCount++;
                case R.id.checkbox_4:
                    checkbox[3].setChecked(true);
                    checkCount++;
                case R.id.checkbox_3:
                    checkbox[2].setChecked(true);
                    checkCount++;
                case R.id.checkbox_2:
                    checkbox[1].setChecked(true);
                    checkCount++;
                case R.id.checkbox_1:
                    checkbox[0].setChecked(true);
                    checkCount++;
            }
        }

    }


    private void initRevealView(Bundle savedInstanceState) {
        revealView.setfillPaintColor(getResources().getColor(R.color.reveal_blue));
        revealView.setOnStateChangeListener(new RevealBackgroundView.OnStateChangeListener() {
            @Override
            public void onStateChange(int state) {
                if (state == RevealBackgroundView.STATE_FINISHED) {
                    //getApplication().setTheme(R.style.AppTheme_Base);
                    rLayoutSaveNote.setBackgroundColor(getResources().getColor(R.color.reveal_blue));

                    profileAnimation();
                }
            }


        });

        if (savedInstanceState == null) {
            final int[] startLocation = getIntent().getIntArrayExtra(ARG_REVEAL_START_LOCATION);

            revealView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    revealView.getViewTreeObserver().removeOnPreDrawListener(this);
                    revealView.startFromLocation(startLocation);
                    return true;
                }
            });
        } else {
            revealView.setToFinishedFrame();
        }

    }

    private void profileAnimation() {
        noteKeepCardView.setVisibility(View.VISIBLE);
        int[] location = new int[2];

        noteSaveCardView.getLocationOnScreen(location);

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(noteKeepCardView, "scaleX", 0f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(noteKeepCardView, "scaleY", 0f, 1f);


        AnimatorSet animset = new AnimatorSet();
        animset.play(scaleX).with(scaleY);
        animset.setDuration(300);
        animset.start();
        animset.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                int height = noteSaveCardView.getMeasuredHeight();
                ObjectAnimator translationY = ObjectAnimator.ofFloat(noteSaveCardView, "translationY", 0f, height * 1.5f);
                translationY.setDuration(300);
                noteSaveCardView.setVisibility(View.VISIBLE);
                translationY.start();
            }
        });


    }

    @Override
    public void onClick(View v) {
        checkbox[0].setChecked(false);
        checkbox[1].setChecked(false);
        checkbox[2].setChecked(false);
        checkbox[3].setChecked(false);
        checkbox[4].setChecked(false);
        checkCount = 0;
        switch (v.getId()) {
            case R.id.checkbox_5:
                checkbox[4].setChecked(true);
                checkCount++;
            case R.id.checkbox_4:
                checkbox[3].setChecked(true);
                checkCount++;
            case R.id.checkbox_3:
                checkbox[2].setChecked(true);
                checkCount++;
            case R.id.checkbox_2:
                checkbox[1].setChecked(true);
                checkCount++;
            case R.id.checkbox_1:
                checkbox[0].setChecked(true);
                checkCount++;
        }
    }
}
